<template>
  <div>
    <!-- Table 展示 -->
    <el-table
      :data="skuList"
      style="width: 100%"
      border=""
    >
      <el-table-column
        type="index"
        label="序号"
        width="80"
        align="center"
      />
      <el-table-column
        prop="skuName"
        label="名称"
        width="width"
      />
      <el-table-column
        prop="skuDesc"
        label="描述"
        width="width"
      />
      <el-table-column
        prop="prop"
        label="默认图片"
        width="110"
      >
        <template slot-scope="{row}">
          <img :src="row.skuDefaultImg" alt="" style="width:80px;height:80px">
        </template>
      </el-table-column>
      <el-table-column
        prop="weight"
        label="重量(KG)"
        width="80"
      />
      <el-table-column
        prop="price"
        label="价格(元)"
        width="80"
      />
      <el-table-column
        prop="prop"
        label="操作"
        width="width"
      >
        <template slot-scope="{row}">
          <el-button v-if="row.isSale ===1" type="success" icon="el-icon-bottom" size="mini" @click="cancelSale(row)" />
          <el-button v-else type="success" icon="el-icon-top" size="mini" @click="onSale(row)" />
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit" />
          <el-button type="info" icon="el-icon-info" size="mini" @click="getSkuInfo(row)" />
          <el-button type="danger" icon="el-icon-delete" size="mini" />
        </template>
      </el-table-column>

    </el-table>
    <!-- 分页器 -->
    <!--
       -->
    <el-pagination
      style="text-align:center"
      :current-page="page"
      :page-sizes="[5, 7, 10]"
      :page-size="limit"
      layout=" prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @current-change="getSkuList"
      @size-change="handleSizeChange"
    />
    <!-- 抽屉 -->
    <el-drawer
      :visible.sync="drawerShow"
      size="50%"
      :show-close="false"
    >
      <el-scrollbar style="height:100%" class="el-drawer__body">
        <el-row>
          <el-col :span="5">名称</el-col>
          <el-col :span="16">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">描述</el-col>
          <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">价格</el-col>
          <el-col :span="16">{{ skuInfo.price }}元</el-col>
        </el-row>
        <el-row>
          <el-col :span="5">平台属性</el-col>
          <el-col :span="16">
            <el-tag v-for="item in skuInfo.skuAttrValueList" :key="item.id" style="margin:10px" type="success">{{ item.attrId }}-{{ item.valueId }}</el-tag>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">商品图片</el-col>
          <el-col :span="16">
            <!-- 轮播图 -->
            <el-carousel height="250px" width="200px">
              <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                <img :src="item.imgUrl" alt="" style="height:250px">
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </el-scrollbar>

    </el-drawer>
  </div>
</template>

<script>
export default {
  name: 'Sku',
  data() {
    return {
      page: 1, // 当前页码数
      limit: 10, // 每页的数量
      skuList: [], // 服务器获取的skuList数据
      total: 0, // skuList数据个数
      drawerShow: false, // 控制抽屉显示与隐藏
      skuInfo: {}// 抽屉展示该sku详情的数据
    }
  },
  mounted() {
    // 挂载时获取服务器skuList数据展示
    this.getSkuList()
  },
  methods: {
    // 获取skuList数据
    async getSkuList(pages = 1) {
      this.page = pages
      const { page, limit } = this
      const result = await this.$API.sku.reqSkuList(page, limit)
      if (result.code === 200) {
        this.skuList = result.data.records
        this.total = result.data.total
      }
    },
    // 改变分页器每页展示个数的回调
    handleSizeChange(limit) {
      this.limit = limit
      this.getSkuList()
    },
    // 下架产品
    async cancelSale(row) {
      const result = await this.$API.sku.reqCancelSale(row.id)
      if (result.code === 200) {
        this.$message({ type: 'success', message: '下架成功' })
        this.getSkuList()
      }
    },
    // 上架产品
    async onSale(row) {
      const result = await this.$API.sku.reqOnSale(row.id)
      if (result.code === 200) {
        this.$message({ type: 'success', message: '上架成功' })
        this.getSkuList()
      }
    },
    // 正在开发中
    edit() {
      this.$message('正在开发中')
    },
    // 查看当前sku详情
    async getSkuInfo(row) {
      // 显示抽屉
      this.drawerShow = true
      // 获取抽屉sku详情数据
      const result = await this.$API.sku.reqSkuInfo(row.id)
      if (result.code === 200) {
        this.skuInfo = result.data
      }
    }
  }
}
</script>

<style>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 150px;
    margin: 0;
  }

  .el-carousel__item:nth-child(2n) {
     background-color: #99a9bf;
  }

  .el-carousel__item:nth-child(2n+1) {
     background-color: #d3dce6;
  }
  .el-carousel__button {
     height: 10px;
     width: 10px;
     border-radius: 5px;
  }
  /*1.显示滚动条：当内容超出容器的时候，可以拖动：*/
  .el-drawer__body {
    overflow: auto;
    /* overflow-x: auto; */
}

</style>
<style scoped>
.el-col{
  margin: 0px 10px 10px 10px;
  line-height: 30px;
}

.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
}

</style>
